package com.midian.common.tag;

import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;

import com.midian.common.system.SysStatic;
import com.midian.model.user.vo.UserInfoVo;

/**
 * 
 * @Title:操作权限控制标签 
 * @Desription:操作权限控制标签，通过传入权限标识名称判断标签体内容是否显示，auth属性为权限唯一名称，多个权限使用，分割
 * @Company:MDKG
 * @ClassName:RoleTag.java
 * @Author:Mengfh
 * @CreateDate:2015-1-27 下午15:15:38  
 * @UpdateUser:Mengfh  
 * @Version:0.1
 */
public class RoleTag extends BodyTagSupport{
	private static final long serialVersionUID = -7545221076814493744L;
	/**权限细项名称*/
	private String auth;
	
	public String getAuth() {
		return auth;
	}
	
	public void setAuth(String auth) {
		this.auth = auth;
	}

	@Override
	public int doEndTag() throws JspException {
		return Tag.EVAL_PAGE;
	}

	/**
	 * 设置登录用户权限
	 */
	@Override
	public int doStartTag() throws JspException {
		UserInfoVo loginUser  = (UserInfoVo) pageContext.getAttribute(SysStatic.MC_LOGIN, PageContext.SESSION_SCOPE);
		if(loginUser != null) {
			//分割权限
			this.auth = this.auth.replace("，", ",");
			String[] auths = this.auth.split(",");
			//超级管理员，不需要进行权限设置
			if(SysStatic.NORMALDELFLAG.equals(loginUser.getIsAdmin())) {
				return Tag.EVAL_BODY_INCLUDE;
			}
			//用户权限集合（key：权限id，value：权限key）
			Map<String, String> authMap = loginUser.getAuthMap();
			if(authMap != null && !authMap.isEmpty()) {
				for (String checkAuth : auths) {
					//如果登录用户权限列表中，包含权限细项列表中的某一项，进行显示
					if(authMap.containsValue(checkAuth)) {
						return Tag.EVAL_BODY_INCLUDE;
					}
				}
			}
		}
		return Tag.SKIP_BODY;
	}
}